#include <stdio.h>
#include <sys/types.h>          /* See NOTES */
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <string.h>
#include <sys/select.h>       /* According to earlier standards */
#include <sys/time.h>
#include <unistd.h>

int main(int argc, const char *argv[])
{
	int sockfd=socket(AF_INET,SOCK_STREAM,0);
	struct sockaddr_in serveraddr,clientaddr;

	serveraddr.sin_family=AF_INET;
	serveraddr.sin_addr.s_addr=inet_addr(argv[1]);
	serveraddr.sin_port=htons(atoi(argv[2]));

	socklen_t addrlen=sizeof(serveraddr);
	bind(sockfd,(struct sockaddr *)&serveraddr,addrlen);
	int num=1;
	listen(sockfd,5);
	char mybuf[32];
	fd_set myreadfds;
	FD_ZERO(&myreadfds);

	while(1)
	{
		FD_SET(0,&myreadfds);
		FD_SET(sockfd,&myreadfds);
		select(sockfd+1,&myreadfds,NULL,NULL,NULL);

		if(1==FD_ISSET(0,&myreadfds))
		{
			printf("plz input >>\n");
			fgets(mybuf,32,stdin);
			mybuf[strlen(mybuf)-1]=='\0';
			printf("myinput:%s",mybuf);
		}
		if(1==FD_ISSET(sockfd,&myreadfds))
		{
			int acceptfd=accept(sockfd,(struct sockaddr *)&clientaddr,&addrlen);
			printf("<<<%d>>>\n",num++);
		}
	}

	return 0;
}
